home *** CD-ROM | disk | FTP | other *** search
-
- #include <string.h>
- #include <stdlib.h>
- #include <float.h>
-
- #if NSTRING_DEBUG > 0
- #include <stdio.h>
- #endif
-
- #include "NString.h"
- #include "NString_Misc.h"
-
- //_________________________________________________________________________________
-
- int NString::SetBufferSizeStep (unsigned long int NewSizeStep)
- {
- if (NewSizeStep == 0)
- return 0;
- BufferSizeStep = NewSizeStep;
- DeallocationMargin = (unsigned long int)(NewSizeStep * DeallocationTrigger);
- return 1;
- }
-
- //_________________________________________________________________________________
-
- unsigned long int NString::GetBufferSizeStep (void)
- {
- return BufferSizeStep;
- }
-
- //_________________________________________________________________________________
-
- int NString::SetDeallocationTrigger (float NewValue)
- {
- if ((NewValue < 0.0) || (NewValue > (0.99 + FLT_EPSILON)))
- return 0;
- DeallocationTrigger = NewValue;
- DeallocationMargin = (unsigned long int)(BufferSizeStep * NewValue);
- return 1;
- }
-
- //_________________________________________________________________________________
-
- float NString::GetDeallocationTrigger (void)
- {
- return DeallocationTrigger;
- }
-
- //_________________________________________________________________________________
-
- NString::NString (void)
- {
- const char *fname = "NString (void)";
-
- if ((sb = new strbody) == NULL)
- OUT_OF_MEM(fname);
- if (! AllocStrBuf(0))
- {
- delete sb;
- sb = NULL;
- OUT_OF_MEM(fname);
- }
- sb->str[0] = '\0';
-
- #if (NSTRING_DEBUG & 2) != 0
- printf("Construction of empty NString.\n");
- #endif
- }
-
- //_________________________________________________________________________________
-
- NString::NString (const char *s)
- {
- const char *fname = "NString (const char *)";
-
- if ((sb = new strbody) == NULL)
- OUT_OF_MEM(fname);
- if ((s != NULL) && (s[0] != '\0')) // non-empty string given -> copy it
- {
- if (! AllocStrBuf(strlen(s)))
- {
- delete sb;
- sb = NULL;
- OUT_OF_MEM(fname);
- }
- strcpy(sb->str, s);
- }
- else // empty string or NULL given -> create empty NString
- {
- if (! AllocStrBuf(0))
- {
- delete sb;
- sb = NULL;
- OUT_OF_MEM(fname);
- }
- sb->str[0] = '\0';
- }
-
- #if (NSTRING_DEBUG & 2) != 0
- printf("Construction of NString \"%s\" from a C-String.\n", sb->str);
- #endif
- }
-
- //_________________________________________________________________________________
-
- NString::NString (const char c)
- {
- const char *fname = "NString (const char)"; // the method's name (for error handling purposes)
-
- if (c == '\0')
- USAGE_ERR(fname, "NString construction from NUL character attempted");
- if ((sb = new strbody) == NULL)
- OUT_OF_MEM(fname);
- if (! AllocStrBuf(1))
- OUT_OF_MEM(fname);
- sb->str[0] = c;
- sb->str[1] = '\0';
-
- #if (NSTRING_DEBUG & 2) != 0
- printf("Construction of NString \"%s\" from a character.\n", sb->str);
- #endif
- }
-
- //_________________________________________________________________________________
-
- NString::~NString (void)
- {
-
- #if (NSTRING_DEBUG & 2) != 0
- printf("Destruction of NString \"%s\"", sb->str);
- #endif
-
- if (sb->refs <= 1)
- {
- free(sb->str);
- delete sb;
-
- #if (NSTRING_DEBUG & 2) != 0
- printf(" - Deleted string body.\n");
- #endif
-
- }
- else
- {
- sb->refs--;
-
- #if (NSTRING_DEBUG & 2) != 0
- printf(".\n");
- #endif
-
- }
- }
-